using System;
using System.Data;
using System.Xml;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;

namespace OracleInternal.ServiceObjects
{
	internal class OraXmlImpl
	{
		internal static string GetRootElement(OracleCommand cmd, OracleXmlType xmlType)
		{
			try
			{
				string result = string.Empty;
				cmd.CommandText = "xmlType.getRootElement";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add(":returnVal", OracleDbType.Varchar2, ParameterDirection.ReturnValue);
				cmd.Parameters[0].DbType = DbType.String;
				cmd.Parameters[0].Size = 1024;
				cmd.Parameters.Add(":self", OracleDbType.XmlType, xmlType, ParameterDirection.Input);
				cmd.ExecuteNonQuery();
				if (cmd.Parameters[0].Value != null || cmd.Parameters[0].Value != DBNull.Value)
				{
					result = (string)cmd.Parameters[0].Value;
				}
				return result;
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static OracleXmlType Extract(OracleCommand cmd, OracleXmlType xmlType, string xpathExpr, string nsMap)
		{
			try
			{
				cmd.CommandText = "xmlType.extract";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add(":returnVal", OracleDbType.XmlType, ParameterDirection.ReturnValue);
				cmd.Parameters.Add(":self", OracleDbType.XmlType, xmlType, ParameterDirection.Input);
				cmd.Parameters.Add(":path", OracleDbType.Varchar2, xpathExpr, ParameterDirection.Input);
				if (!string.IsNullOrEmpty(nsMap))
				{
					cmd.Parameters.Add(":nsmap", OracleDbType.Varchar2, nsMap, ParameterDirection.Input);
				}
				cmd.ExecuteNonQuery();
				if (cmd.Parameters[0].Value == null || cmd.Parameters[0].Value != DBNull.Value)
				{
					return OracleXmlType.Null;
				}
				return (OracleXmlType)cmd.Parameters[0].Value;
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static OracleXmlType Extract(OracleCommand cmd, OracleXmlType xmlType, string xpathExpr, XmlNamespaceManager nsMgr)
		{
			string nsMap = DotNetXmlImpl.NsMgrToString(nsMgr);
			return Extract(cmd, xmlType, xpathExpr, nsMap);
		}

		internal static bool IsExists(OracleCommand cmd, OracleXmlType xmlType, string xpathExpr, string nsMap)
		{
			try
			{
				cmd.CommandText = "xmlType.existsNode";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add(":returnVal", OracleDbType.Int32, ParameterDirection.ReturnValue);
				cmd.Parameters[0].DbType = DbType.Int32;
				cmd.Parameters.Add(":self", OracleDbType.XmlType, xmlType, ParameterDirection.Input);
				cmd.Parameters.Add(":path", OracleDbType.Varchar2, xpathExpr, ParameterDirection.Input);
				if (!string.IsNullOrEmpty(nsMap))
				{
					cmd.Parameters.Add(":nsmap", OracleDbType.Varchar2, nsMap, ParameterDirection.Input);
				}
				cmd.ExecuteNonQuery();
				return ((int)cmd.Parameters[0].Value > 0) ? true : false;
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static bool IsExists(OracleCommand cmd, OracleXmlType xmlType, string xpathExpr, XmlNamespaceManager nsMgr)
		{
			string nsMap = DotNetXmlImpl.NsMgrToString(nsMgr);
			return IsExists(cmd, xmlType, xpathExpr, nsMap);
		}

		internal static bool IsFragment(OracleCommand cmd, OracleXmlType xmlType)
		{
			try
			{
				cmd.CommandText = "xmlType.IsFragment";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add(":returnVal", OracleDbType.Int32, ParameterDirection.ReturnValue);
				cmd.Parameters[0].DbType = DbType.Int32;
				cmd.Parameters.Add(":self", OracleDbType.XmlType, xmlType, ParameterDirection.Input);
				cmd.ExecuteNonQuery();
				return ((int)cmd.Parameters[0].Value > 0) ? true : false;
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static OracleXmlType Transform(OracleCommand cmd, OracleXmlType xmlType, OracleXmlType xslDoc, string paramMap)
		{
			try
			{
				cmd.CommandText = "xmlType.transform";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add(":returnVal", OracleDbType.XmlType, ParameterDirection.ReturnValue);
				cmd.Parameters.Add(":self", OracleDbType.XmlType, xmlType, ParameterDirection.Input);
				cmd.Parameters.Add(":xsldoc", OracleDbType.XmlType, xslDoc, ParameterDirection.Input);
				if (!string.IsNullOrEmpty(paramMap))
				{
					cmd.Parameters.Add(":parammap", OracleDbType.Varchar2, paramMap, ParameterDirection.Input);
				}
				cmd.ExecuteNonQuery();
				if (cmd.Parameters[0].Value != null || cmd.Parameters[0].Value != DBNull.Value)
				{
					return (OracleXmlType)cmd.Parameters[0].Value;
				}
				return OracleXmlType.Null;
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static void Update(OracleCommand cmd, OracleXmlType xmlType, string xpathExpr, string nsMap, string val)
		{
			try
			{
				cmd.CommandText = "UpdateXml";
				cmd.CommandType = CommandType.StoredProcedure;
				OracleParameter param = new OracleParameter(":XMLType_instance", OracleDbType.XmlType, xmlType, ParameterDirection.Input);
				cmd.Parameters.Add(param);
				OracleParameter param2 = new OracleParameter(":XPath_string", OracleDbType.Varchar2, xpathExpr, ParameterDirection.Input);
				cmd.Parameters.Add(param2);
				OracleParameter param3 = new OracleParameter(":value_expr", OracleDbType.Varchar2, val, ParameterDirection.Input);
				cmd.Parameters.Add(param3);
				if (!string.IsNullOrEmpty(nsMap))
				{
					OracleParameter param4 = new OracleParameter(":namespace_string", OracleDbType.Varchar2, nsMap, ParameterDirection.Input);
					cmd.Parameters.Add(param4);
				}
				cmd.ExecuteNonQuery();
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static void Update(OracleCommand cmd, OracleXmlType xmlType, string xpathExpr, XmlNamespaceManager nsMgr, string val)
		{
			string nsMap = DotNetXmlImpl.NsMgrToString(nsMgr);
			Update(cmd, xmlType, xpathExpr, nsMap, val);
		}

		internal static void Update(OracleCommand cmd, OracleXmlType xmlType, string xpathExpr, string nsMap, OracleXmlType newXmlTypeVal)
		{
			try
			{
				cmd.CommandText = "UpdateXml";
				cmd.CommandType = CommandType.StoredProcedure;
				OracleParameter param = new OracleParameter(":XMLType_instance", OracleDbType.XmlType, xmlType, ParameterDirection.Input);
				cmd.Parameters.Add(param);
				OracleParameter param2 = new OracleParameter(":XPath_string", OracleDbType.Varchar2, xpathExpr, ParameterDirection.Input);
				cmd.Parameters.Add(param2);
				OracleParameter param3 = new OracleParameter(":value_expr", OracleDbType.XmlType, newXmlTypeVal, ParameterDirection.Input);
				cmd.Parameters.Add(param3);
				if (!string.IsNullOrEmpty(nsMap))
				{
					OracleParameter param4 = new OracleParameter(":namespace_string", OracleDbType.Varchar2, nsMap, ParameterDirection.Input);
					cmd.Parameters.Add(param4);
				}
				cmd.ExecuteNonQuery();
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static void Update(OracleCommand cmd, OracleXmlType xmlType, string xpathExpr, XmlNamespaceManager nsMgr, OracleXmlType newXmlTypeVal)
		{
			string nsMap = DotNetXmlImpl.NsMgrToString(nsMgr);
			Update(cmd, xmlType, xpathExpr, nsMap, newXmlTypeVal);
		}

		internal static bool Validate(OracleCommand cmd, OracleXmlType xmlType, string schemaUrl)
		{
			try
			{
				cmd.CommandText = "xmlType.isSchemaValid";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add(":returnVal", OracleDbType.Int32, ParameterDirection.ReturnValue);
				cmd.Parameters[0].DbType = DbType.Int32;
				cmd.Parameters.Add(":self", OracleDbType.XmlType, xmlType, ParameterDirection.Input);
				cmd.Parameters.Add(":schurl", OracleDbType.Varchar2, schemaUrl, ParameterDirection.Input);
				cmd.ExecuteNonQuery();
				return ((int)cmd.Parameters[0].Value > 0) ? true : false;
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static string GetSchemaURL(OracleCommand cmd, OracleXmlType xmlType)
		{
			try
			{
				cmd.CommandText = "xmlType.getSchemaURL";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add(":returnVal", OracleDbType.Varchar2, ParameterDirection.ReturnValue);
				cmd.Parameters[0].Size = 1024;
				cmd.Parameters[0].DbType = DbType.String;
				cmd.Parameters.Add(":self", OracleDbType.XmlType, xmlType, ParameterDirection.Input);
				cmd.ExecuteNonQuery();
				if (cmd.Parameters[0].Value == null || cmd.Parameters[0].Value == DBNull.Value)
				{
					return string.Empty;
				}
				return (string)cmd.Parameters[0].Value;
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static void GetSchema(OracleCommand cmd, OracleXmlType xmlType, string schemaUrl, out OracleClob schemaInfo, out byte[] schemaId)
		{
			try
			{
				schemaId = null;
				schemaInfo = null;
				cmd.CommandText = "select c.schema_id, c.schema.getclobval() from user_xml_schemas c where schema_url=:x";
				cmd.CommandType = CommandType.Text;
				cmd.Parameters.Add(":x", OracleDbType.Varchar2, schemaUrl, ParameterDirection.Input);
				OracleDataReader oracleDataReader = cmd.ExecuteReader();
				if (oracleDataReader.Read() && !oracleDataReader.IsDBNull(0))
				{
					schemaId = new byte[16];
					oracleDataReader.GetBytes(0, 0L, schemaId, 0, 16);
					schemaInfo = oracleDataReader.GetOracleClob(1);
				}
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}

		internal static void GetSchema(OracleCommand cmd, OracleXmlType xmlType, byte[] schemaId, out OracleClob schemaInfo, out string schemaUrl)
		{
			try
			{
				schemaUrl = null;
				schemaInfo = null;
				cmd.CommandText = "select c.schema_url, c.schema.getclobval() from user_xml_schemas c where schema_id=:x";
				cmd.CommandType = CommandType.Text;
				cmd.Parameters.Add(":x", OracleDbType.Raw, schemaId, ParameterDirection.Input);
				OracleDataReader oracleDataReader = cmd.ExecuteReader();
				if (oracleDataReader.Read() && !oracleDataReader.IsDBNull(0))
				{
					schemaUrl = oracleDataReader.GetString(0);
					schemaInfo = oracleDataReader.GetOracleClob(1);
				}
			}
			finally
			{
				cmd.Parameters.Clear();
			}
		}
	}
}
